/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WidokPokazPlany.java
 *
 * Created on 2011-07-19, 21:40:55
 */

package App.widoki;

import App.kontrolery.KontrolerPokazPlany;
import App.modele.ModelPokazPlany;
import java.util.List;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import skladowisko.transport.Transport;
import skladowisko.transport.TransportPrzychodzacy;
import skladowisko.transport.TransportWychodzacy;

/**
 *
 * @author Krzysiek
 */
public class WidokPokazPlany extends javax.swing.JFrame implements TreeSelectionListener {

    /** Creates new form WidokPokazPlany */

    private KontrolerPokazPlany kontroler = null;
    private ModelPokazPlany model = null;

    private DefaultTreeModel treeModel = null;

    public WidokPokazPlany(KontrolerPokazPlany kontroler, ModelPokazPlany model) {
        this.kontroler = kontroler;
        this.model = model;
        
        initComponents();
        initTreeModel(model.getPrzychodzace(), model.getWychodzace());
        jTree1.addTreeSelectionListener(this);

    }

    public void initTreeModel(List<TransportPrzychodzacy> przychodzace, List<TransportWychodzacy> wychodzace)
    {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Planowane transporty");
        treeModel = new DefaultTreeModel(root);

        DefaultMutableTreeNode nodePrzychodzace = new DefaultMutableTreeNode("Przychodzące");
        DefaultMutableTreeNode nodeWychodzace = new DefaultMutableTreeNode("Wychodzące");
        
        root.add(nodePrzychodzace);
        dodajWezly(nodePrzychodzace, przychodzace);
        root.add(nodeWychodzace);
        dodajWezly(nodeWychodzace, wychodzace);

        jTree1.setModel(treeModel);
    }

    private void dodajWezly(DefaultMutableTreeNode node, List<?> obiekty)
    {
        for(Object o : obiekty)
        {
            node.add(new DefaultMutableTreeNode(o));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaOpisTransportu = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Wykaz planowanych transportów");

        jScrollPane1.setViewportView(jTree1);

        jTextAreaOpisTransportu.setColumns(20);
        jTextAreaOpisTransportu.setRows(5);
        jTextAreaOpisTransportu.setText("Nie  wybrano żadnego transportu!");
        jScrollPane2.setViewportView(jTextAreaOpisTransportu);

        jLabel1.setText("Opis wybranego transportu:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addGap(1, 1, 1)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new WidokPokazPlany().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextAreaOpisTransportu;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables

    public void valueChanged(TreeSelectionEvent e)
    {
       DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                       jTree1.getLastSelectedPathComponent();


       Object obiekt = (node.getUserObject());

       if(obiekt instanceof Transport)
       {
           Transport tmp = (Transport)obiekt;
           jTextAreaOpisTransportu.setText(tmp.pokazSzczegoly());
       }
       else
       {
            jTextAreaOpisTransportu.setText("Nie wybrano transportu");
       }
    }

}
